package com.health.viewscontrols.focusgroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import com.health.viewscontrols.NovaFragmentActivity;
import com.health.viewscontrols.PersonalFileActivity.onFragmentSave;
import com.health.R;
import com.health.adapter.FragmentAdapter;
import com.health.fragment.focusgroup.child.CheckFragment1_1;
import com.health.fragment.focusgroup.child.CheckFragment1_2;
import com.health.fragment.focusgroup.child.CheckFragment1_3;
import com.health.fragment.focusgroup.child.CheckFragment1_4;
import com.health.fragment.focusgroup.child.CheckFragment2_1;
import com.health.fragment.focusgroup.child.CheckFragment2_2;
import com.health.fragment.focusgroup.child.CheckFragment2_3;
import com.health.fragment.focusgroup.child.CheckFragment2_4;
import com.health.fragment.focusgroup.child.CheckFragment3_1;
import com.health.fragment.focusgroup.child.CheckFragment3_2;
import com.health.fragment.focusgroup.child.CheckFragment3_3;
import com.health.fragment.focusgroup.child.CheckFragment3_4;
import com.health.fragment.focusgroup.child.ErTongGuanLiKaFragment;
import com.health.fragment.focusgroup.child.XinShengErFangShiFragment;
import com.health.model.Disease;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;

/**
 * 重点人群--儿童
 * 
 * @author nova
 *
 */
public class ChildActivity extends NovaFragmentActivity implements OnClickListener {

	private ViewPager viewPager;
	private ArrayList<Fragment> fragments;
	private TextView title;
	public Button btn_up;// 保存
	public Button btn_back;// 返回
	private ExpandableListView list;
	private ChildAdapter Childadapter;
	FragmentAdapter fragmentAdapter;
	public static Activity mChildActivity = null;

	// android:id="@+id/add_list
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		// WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.focusgroup_child_main);
		initView();
		/** 使用适配器 */
		addListener();
		 
	}

	private void initView() {
		title = (TextView) findViewById(R.id.title);
		title.setText("重点人群--儿童");

		list = (ExpandableListView) findViewById(R.id.addfamily_list);
		Childadapter = new ChildAdapter(this);
		list.setAdapter(Childadapter);
		Childadapter.setB1(true);

		btn_up = (Button) findViewById(R.id.btn_up);
		btn_back = (Button) findViewById(R.id.btn_back);
		viewPager = (ViewPager) findViewById(R.id.viewpager);
		btn_back.setOnClickListener(this);
		btn_up.setOnClickListener(this);
		mChildActivity = this;
	}

	private void addListener() {

		Fragment etglk = new ErTongGuanLiKaFragment();
		Fragment xsrfsf = new XinShengErFangShiFragment();

		Fragment cqsfFragment1 = new CheckFragment1_1();
		Fragment cqsfFragment2 = new CheckFragment1_2();
		Fragment cqsfFragment3 = new CheckFragment1_3();
		Fragment cqsfFragment4 = new CheckFragment1_4();

		Fragment cqsfFragment5 = new CheckFragment2_1();
		Fragment cqsfFragment6 = new CheckFragment2_2();
		Fragment cqsfFragment7 = new CheckFragment2_3();
		Fragment cqsfFragment8 = new CheckFragment2_4();

		Fragment cqsfFragment9 = new CheckFragment3_1();
		Fragment cqsfFragment10 = new CheckFragment3_2();
		Fragment cqsfFragment11 = new CheckFragment3_3();
		Fragment cqsfFragment12 = new CheckFragment3_4();

		fragments = new ArrayList<Fragment>();
		fragments.add(etglk);
		fragments.add(xsrfsf);
		fragments.add(cqsfFragment1);
		fragments.add(cqsfFragment2);
		fragments.add(cqsfFragment3);
		fragments.add(cqsfFragment4);
		fragments.add(cqsfFragment5);
		fragments.add(cqsfFragment6);
		fragments.add(cqsfFragment7);
		fragments.add(cqsfFragment8);
		fragments.add(cqsfFragment9);
		fragments.add(cqsfFragment10);
		fragments.add(cqsfFragment11);
		fragments.add(cqsfFragment12);
		fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
		viewPager.setAdapter(fragmentAdapter);
		// btn_back.setOnClickListener(this);
		// btn_up.setOnClickListener(this);
		list.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition,
					long id) {
				// Intent intent = new Intent(getActivity(),
				// PlayActivity.class);
				// intent.setData(Uri.parse(adapter.getList().get(groupPosition).videos.get(childPosition).url));
				// startActivity(intent);
				// "儿童管理", "新生儿访视", "1岁内检查", "1-2岁内检查", "3-6岁内检查"
				// if(groupPosition==2){
				// //1岁内检查
				// viewPager.setCurrentItem(childPosition);
				// }else if(groupPosition==3){
				// viewPager.setCurrentItem(4+childPosition);
				// }else if(groupPosition==4){
				// viewPager.setCurrentItem(8+childPosition);
				// }
				viewPager.setCurrentItem(childPosition);

				return true;
			}
		});

		viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {
				// adapter.getItemId(position)
				// adapter.set
				// adapter.set
				// adapter.setCheck(1);

			}

			@Override
			public void onPageScrollStateChanged(int state) {
				// Childadapter.setCheck(state);
			}
		});

	}

	@Override
	public void onClick(View v) {

		int id = v.getId();
		if (id == R.id.btn_back) {
			this.finish();
		} else if (id == R.id.btn_up) {
			// 保存
			onFragmentSave currentFragment = (onFragmentSave) fragmentAdapter.getCurrentFragment();
			currentFragment.save();
		} else {
		}

	}

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//////////////////////// 下拉按钮Adapter/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	/**
	 * 添加家族史adapter
	 */
	@SuppressLint("UseSparseArrays")
	public class ChildAdapter extends BaseExpandableListAdapter implements OnCheckedChangeListener, OnClickListener {
		private String[] types = { "儿童管理卡", "新生儿访视", "1岁内检查", "1-2岁内检查", "3-6岁内检查" };
		private HashMap<Integer, CheckBox> hm = new HashMap<Integer, CheckBox>();
		private String[][] inits = { {}, {}, { "2,满月健康检查记录表", "3,3月龄健康检查记录表", "4,6月龄健康检查记录表", "5,8月龄健康检查记录表" },
				{ "6,12月龄健康检查记录表", "7,18月龄健康检查记录表", "8,24月龄健康检查记录表", "9,30月龄健康检查记录表" },
				{ "10,3岁健康检查记录表", "11,4岁健康检查记录表", "12,5岁健康检查记录表", "13,6岁健康检查记录表" } };
		/** 是否为滑动 */
		private boolean s = false;

		private Context mContext;
		public ArrayList<ArrayList<Disease>> dss = new ArrayList<ArrayList<Disease>>();
		private Disease disease;

		public ChildAdapter(Context mContext) {
			super();
			SparseArray<Integer> sa = new SparseArray<Integer>();
			this.mContext = mContext;

			for (int i = 0; i < inits.length; i++) {
				ArrayList<Disease> ds = new ArrayList<Disease>();
				for (int j = 0; j < inits[i].length; j++) {
					String[] ss = inits[i][j].split(",");
					ds.add(new Disease(ss[0], ss[1]));
				}
				dss.add(ds);
			}
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem_addpast_group, null);
				holder = new ViewHolder();
				findGroupView(convertView, holder);
				convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
			} else
				holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);

			setGroupData(holder, groupPosition, isExpanded);

			return convertView;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
				ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem_addpast_child, null);
				holder = new ViewHolder();
				findChildView(convertView, holder);
				convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
			} else {
				holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
			}

			Disease d = dss.get(groupPosition).get(childPosition);
			setChildData(holder, d, isLastChild);

			return convertView;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return false;
		}

		private boolean b1;
		private boolean b2;

		private void setGroupData(final ViewHolder h, int position, boolean isExpanded) {
			String str = types[position];
			h.groupText.setText(str);

			Log.w("eeeeeeeeeeeeeexpalnd---", "pos"+position);
			if (position == 0) {
				hm.put(0, h.groupText);
				h.groupText.setOnCheckedChangeListener(null);
				h.groupText.setOnCheckedChangeListener(this);
				h.groupText.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						h.groupText.setChecked(true);
						b2 = false;
					}
				});
				h.groupText.setTag(R.id.TAG, 0);
				if (b1) {
					h.groupText.setBackgroundColor(Color.GRAY);
				} else {
					h.groupText.setBackgroundColor(Color.WHITE);
				}
				h.groupText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
			} else if (position == 1) {
				hm.put(1, h.groupText);
				h.groupText.setOnCheckedChangeListener(null);
				h.groupText.setOnCheckedChangeListener(this);
				h.groupText.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						h.groupText.setChecked(true);

						b1 = false;
					}
				});
				h.groupText.setTag(R.id.TAG, 1);
				if (b2) {
					h.groupText.setBackgroundColor(Color.GRAY);
				} else {
					h.groupText.setBackgroundColor(Color.WHITE);
				}
			}else {
				h.groupText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
				h.groupText.setClickable(false);// disabled
				h.groupText.setFocusable(false);
				if (isExpanded) {
					h.groupText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.up_arrow, 0);
				} else {
					h.groupText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.down_arrow, 0);
				}
			}
			

		}

		@SuppressLint("NewApi")
		private void setChildData(final ViewHolder h, Disease d, boolean isLastChild) {
			if (isLastChild)
				h.line.setVisibility(View.VISIBLE);
			else
				h.line.setVisibility(View.GONE);

			h.box.setText(d.getName());
			h.box.setOnCheckedChangeListener(null);
			h.box.setChecked(d.isChecked());
			h.box.setTag(R.id.TAG, d);
			h.box.setOnCheckedChangeListener(this);
			h.box.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					h.box.setChecked(true);
				}
			});
			hm.put(Integer.parseInt(d.getCode()), h.box);

			if (d.isChecked()) {
				// h.hide.setVisibility(View.VISIBLE);
				h.box.setBackground(ChildActivity.this.getResources().getDrawable(R.drawable.erjixuanzhong));// kgroundColor(Color.DKGRAY);
			} else {
				// h.hide.setVisibility(View.GONE);
				h.box.setBackgroundColor(Color.WHITE);
			}
		}

		/** 滑动切换 */
		public void setCheck(int i) {
			try {
				s = true;
				Set<Integer> set = hm.keySet();
				for (int key : set) {
					hm.get(key).setChecked(false);
				}
				hm.get(i).setChecked(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		private class ViewHolder {
			CheckBox groupText;

			View line;
			CheckBox box;
		}

		private void findGroupView(View v, ViewHolder h) {
			h.groupText = (CheckBox) v.findViewById(R.id.addpast_group_text);
		}

		private void findChildView(View v, ViewHolder h) {
			h.line = v.findViewById(R.id.addpast_child_line);
			h.box = (CheckBox) v.findViewById(R.id.addpast_child_box);
		}

		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			for (ArrayList<Disease> bdchecks : dss) {
				for (Disease ds : bdchecks) {
					ds.setChecked(false);
				}
			}
			String className = buttonView.getTag(R.id.TAG).getClass().getCanonicalName();

			System.out.println(className);
			if (className.equals("java.lang.Integer")) {

				System.out.println(isChecked);
				// notifyDataSetChanged();
				int i = (Integer) buttonView.getTag(R.id.TAG);
				if (i == 0) {
					b1 = isChecked;
					if (isChecked) {
						viewPager.setCurrentItem(0);
					}
				} else if (i == 1) {
					b2 = isChecked;
					if (isChecked) {
						viewPager.setCurrentItem(1);
					}
				}
				notifyDataSetChanged();
				return;
			} else {
				Disease dd = (Disease) buttonView.getTag(R.id.TAG);
				if (isChecked) {
					b1 = false;
					b2 = false;
					viewPager.setCurrentItem(Integer.parseInt(dd.getCode()));
					dd.setChecked(isChecked);
					notifyDataSetChanged();
				} else {
					dd.setChecked(isChecked);
					notifyDataSetChanged();
				}
			}
		}

		@Override
		public int getGroupCount() {
			return types.length;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return dss.get(groupPosition).size();
		}

		@Override
		public Object getGroup(int groupPosition) {
			return null;
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return null;
		}

		@Override
		public long getGroupId(int groupPosition) {
			return 0;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return 0;
		}

		@Override
		public boolean hasStableIds() {
			return false;
		}

		@Override
		public void onClick(View v) {

		}

		public boolean isB1() {
			return b1;
		}

		public void setB1(boolean b1) {
			this.b1 = b1;
		}
		
		
	}

}